package com.common.utils.tools.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.sql.Driver;

public class BaseGenerator {

    public static void moduleGenerator(String module,String [] tableNames,String projectName,String [] tablePrefix,String name){

        GlobalConfig globalConfig = getGlobalConfig(module,projectName);// 全局配置

        DataSourceConfig dataSourceConfig = getDataSourceConfig();// 数据源配置

        PackageConfig packageConfig = getPackageConfig(module,name);// 包配置

        StrategyConfig strategyConfig = getStrategyConfig(tableNames,tablePrefix);// 策略配置

        TemplateConfig templateConfig = getTemplateConfig(module);// 配置模板

        new AutoGenerator()
                .setGlobalConfig(globalConfig)
                .setDataSource(dataSourceConfig)
                .setPackageInfo(packageConfig)
                .setStrategy(strategyConfig)
                .setTemplate(templateConfig)
                .execute();

    }

    private static TemplateConfig getTemplateConfig(String module) {
        TemplateConfig templateConfig = new TemplateConfig();
        if (module.contains("service")){
            //templateConfig.setEntity(new TemplateConfig().getEntity(false))
            templateConfig.setEntity(null)
                    //.setMapper(new TemplateConfig().getMapper())
                    //.setXml(new TemplateConfig().getXml())
                    .setMapper(null)//mapper模板采用mybatis-plus自己模板
                    .setXml(null)
                    .setService(new TemplateConfig().getService())
                    .setServiceImpl(new TemplateConfig().getServiceImpl())
                    .setController(null);//service模块不生成controller代码
        }else if (module.contains("mapper")){//web模块只生成controller代码
            templateConfig.setEntity(null)
                    .setMapper(new TemplateConfig().getMapper())
                    .setXml(new TemplateConfig().getXml())
                    .setService(null)
                    .setServiceImpl(null)
                    .setController(null);
        }else if(module.contains("domain")){
            templateConfig.setEntity(new TemplateConfig().getEntity(false))
                    .setMapper(null)//mapper模板采用mybatis-plus自己模板
                    .setXml(null)
                    .setService(null)
                    .setServiceImpl(null)
                    .setController(null);//service模块不生成controller代码
        }else throw new IllegalArgumentException("参数匹配错误，请检查");
        return templateConfig;
    }

    private static StrategyConfig getStrategyConfig(String[] tableNames,String [] tablePrefix) {
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig

                .setCapitalMode(true)//驼峰命名
                .setEntityLombokModel(false)
                .setRestControllerStyle(false)
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude(tableNames)
                .setTablePrefix(tablePrefix);// 此处可以修改为您的表前缀;
        return strategyConfig;
    }

    private static PackageConfig getPackageConfig(String module,String name) {
        PackageConfig packageConfig = new PackageConfig();
        String packageName = "com.cloud";//不同模块 代码生成具体路径自定义指定
        if ("service".contains(module)){
            packageName+=".service";
        }else if ("domain".contains(module)){
            packageName+=".domain";
        }else if("mapper".contains(module)){
            packageName+=".mapper";
        }
        if(StringUtils.isNotBlank(name)){
            packageConfig.setParent(packageName)
                    .setEntity("domain."+name)
                    .setMapper("mapper."+name)
                    .setXml("mapper."+name)
                    .setService("service."+name)
                    .setServiceImpl("service."+name+".impl")
                    .setController("controller."+name);
        }else {
            packageConfig.setParent(packageName)
                    .setEntity("domain")
                    .setMapper("mapper")
                    .setXml("mapper")
                    .setServiceImpl("service.impl")
                    .setService("service")
                    .setController("controller");
        }


        return packageConfig;
    }

    private static DataSourceConfig getDataSourceConfig() {
        String dbUrl = "jdbc:mysql://localhost:3306/cloud_test?useUnicode=true&useSSL=false&characterEncoding=utf8";
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setDriverName(Driver.class.getName())
                .setUsername("root")
                .setPassword("123456")
                .setUrl(dbUrl);
        return dataSourceConfig;
    }

    private static GlobalConfig getGlobalConfig(String module,String projectName) {
        String url = projectName+"/"+module;
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOpen(false)//new File(module).getAbsolutePath()得到模块根目录路径，因事Maven项目，代码指定路径自定义调整

                .setOutputDir(new File(url).getAbsolutePath()+"/src/main/java")//生成文件的输出目录
                .setFileOverride(true)//是否覆盖已有文件
                .setBaseResultMap(true)
                .setBaseColumnList(true)
                .setActiveRecord(false)
                .setAuthor("liuyuan")
                .setServiceName("%sService");
        return globalConfig;
    }
}
